package com.helloae.chap09;

import java.io.File;
import java.io.IOException;
import java.net.UnknownHostException;

import com.esri.arcgis.carto.FeatureLayer;
import com.esri.arcgis.datasourcesfile.ShapefileWorkspaceFactory;
import com.esri.arcgis.geodatabase.ICursor;
import com.esri.arcgis.geodatabase.ICursorProxy;
import com.esri.arcgis.geodatabase.IFeatureClass;
import com.esri.arcgis.geodatabase.IFeatureWorkspace;
import com.esri.arcgis.system.AoInitialize;
import com.esri.arcgis.system.EngineInitializer;
import com.esri.arcgis.system.IStatisticsResults;
import com.esri.arcgis.system.esriLicenseProductCode;

/**
 * 查找一个字段中的统计值，包括最大值、最小值、平均值。<br>
 * 附：与ArcToolbox--Analysis Tools--Statistics--Summary Statistics功能相同。<br>
 * 示例章节：9.6.1
 * @author zhushijie
 */
public class DataStatistics {

	public static void main(String[] args) throws UnknownHostException, IOException {
		//初始化AE许可
		EngineInitializer.initializeVisualBeans();
		AoInitialize ao = new AoInitialize();
		ao.initialize(esriLicenseProductCode.esriLicenseProductCodeEngine);

		//导入数据
		ShapefileWorkspaceFactory pWF = new ShapefileWorkspaceFactory();
		String ecliseWorkspace = System.getenv("Eclipse3.6Workspace");
		IFeatureWorkspace pFeatureWorkspace = (IFeatureWorkspace) pWF
				.openFromFile(ecliseWorkspace + "HelloAE" + File.separator
						+ "DataSet" + File.separator + "chap09", 0);

		IFeatureClass pCountyClass = pFeatureWorkspace
				.openFeatureClass("idcounty.shp");
		FeatureLayer pCountyLayer = new FeatureLayer();
		pCountyLayer.setFeatureClassByRef(pCountyClass);
		
		ICursor pCursor = new ICursorProxy(pCountyLayer.search(null, false));
		com.esri.arcgis.geodatabase.DataStatistics pData = new com.esri.arcgis.geodatabase.DataStatistics();
		pData.setField("change");
		pData.setCursorByRef(pCursor);
		IStatisticsResults pStatisticsResults = pData.getStatistics();
		System.out.println("Maximum: " + pStatisticsResults.getMaximum());
		System.out.println("Mean: " + pStatisticsResults.getMean());
		System.out.println("Minimum: " + pStatisticsResults.getMinimum());
		
		ao.shutdown();
		System.out.println("Congratulaction!");
	}

}
